package com.easylinkin.linkappapi.operatelog.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.operatelog.entity.CommonOperateLog;
import com.easylinkin.linkappapi.operatelog.mapper.CommonOperateLogMapper;
import com.easylinkin.linkappapi.operatelog.service.CommonOperateLogService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tongjie
 * @since 2020-05-07
 */
@Service
public class CommonOperateLogServiceImpl extends ServiceImpl<CommonOperateLogMapper, CommonOperateLog> implements CommonOperateLogService {

    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public boolean addLog(CommonOperateLog commonOperateLog) {
        commonOperateLog.setCreateTime(new Date());
//        if (commonOperateLog.getUserAccount() == null || commonOperateLog.getTenantId() == null) {
//            LinkappUser current = null;
//            try {
//                current = linkappUserContextProducer.getCurrent();
//            } catch (Exception e) {
//                e.printStackTrace();
//                log.error(e.toString());
//            }
//            
//            if (current != null && commonOperateLog.getUserAccount() == null) {
//                commonOperateLog.setUserAccount(current.getUsername());
//            }
//            if (current != null && commonOperateLog.getTenantId() == null) {
//                commonOperateLog.setTenantId(current.getTenantId());
//            }
//        }
        return save(commonOperateLog);
    }

    @Override
    public IPage<CommonOperateLog> getPage(Page page, CommonOperateLog operateLog) {
        operateLog.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return baseMapper.selectLogs(page, operateLog);
    }

    @Override
    public void deleteBatch(CommonOperateLog commonOperateLog) {
        Assert.notNull(commonOperateLog, "参数为空");
        Assert.notNull(commonOperateLog.getQueryTimeStart(), "开始时间参数为空");
        Assert.notNull(commonOperateLog.getQueryTimeEnd(), "结束时间参数为空");
        commonOperateLog.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        baseMapper.deleteLogs(commonOperateLog);
    }

}
